<?php

declare(strict_types=1);

namespace app\service\develop;

use app\model\develop\ApplyCenterModel;
use app\model\develop\ApplyMapModel;
use owns\auth\UserAuth;

class ApplyService
{
    /**
     * 应用图库列表
     * @param array $param
     */
    public function mapList(array $param): array
    {
        $list = ApplyMapModel::custom($param)
            ->order('sort asc, id desc')
            ->pages($param)
            ->select();
        return list_fmt($list, ApplyMapModel::custom($param)->count());
    }
    /**
     * 应用图库编辑
     * @param array $data
     * @param integer $id
     */
    public function mapEdit(array $data, int $id)
    {
        $auth = UserAuth::instance();
        if ($id) {
            $maps = ApplyMapModel::find($id);
            if ($maps) {
                tips('未找到图片信息，修改失败');
            }
            $maps->updated_by = $auth->username();
        } else {
            $maps = new ApplyMapModel();
            $maps->created_by = $auth->username();
        }
        $maps->location = $data['location'];
        $maps->title = trim($data['title']);
        $maps->image = $data['image'];
        $maps->jump_type = $data['jump_type'];
        $maps->target = $maps->jump_type == 0 ? '' : $data['target'];
        $maps->sort = $data['sort'];
        $maps->status = $data['status'];
        return $maps->save();
    }
    /**
     * 应用图库状态修改
     * @param array $id
     */
    public function mapStatus(array $id)
    {
        $maps = ApplyMapModel::find($id);
        if ($maps) {
            tips('未找到图片信息，修改失败');
        }
        $maps->updated_by = UserAuth::instance()->username();
        $maps->status = $maps->status == 1 ? 0 : 1;
        return $maps->save();
    }
    /**
     * 应用图库删除
     * @param array $ids
     */
    public function mapDelete(array $ids)
    {
        return ApplyMapModel::whereIn('id', $ids)->delete();
    }

    /**
     * 应用中心列表
     */
    public function centerList(array $param): array
    {
        $list = ApplyCenterModel::custom($param)
            ->order('sort asc,id desc')
            ->pages($param)
            ->select();
        return list_fmt($list, ApplyCenterModel::custom($param)->count());
    }
    /**
     * 应用中心编辑
     * @param array $data
     * @param integer $id
     */
    public function centerEdit(array $data, int $id)
    {
        $auth = UserAuth::instance();
        if ($id) {
            $maps = ApplyCenterModel::find($id);
            if ($maps) {
                tips('未找到应用信息，修改失败');
            }
            $maps->updated_by = $auth->username();
        } else {
            $maps = new ApplyCenterModel();
            $maps->created_by = $auth->username();
        }
        $maps->location = $data['location'];
        $maps->name = trim($data['name']);
        $this->_centerRepeat($maps->location, $maps->name, 1, $id);
        $maps->tag = trim($data['tag']);
        $this->_centerRepeat($maps->location, $maps->tag, 0, $id);
        $maps->icon = $data['icon'];
        $maps->jump_type = $data['jump_type'];
        $maps->target = $maps->jump_type == 0 ? '' : $data['target'];
        $maps->sort = $data['sort'];
        $maps->status = $data['status'];
        return $maps->save();
    }
    /**
     * 应用查重
     * @param string $location 位置
     * @param string $keyword 名称
     * @param integer $type 类型：0标签  1名称
     * @param integer $id 应用ID
     */
    private function _centerRepeat($location, $keyword, $type, $id)
    {
        $where = ApplyCenterModel::where('location', $location)
            ->where($type == 1 ? 'name' : 'tag', $keyword);
        if ($id) {
            $where->where('id', '<>', $id);
        }
        if ($where->find()) {
            tips(('应用' . $type ? '名称' : '标签') . '已被使用，请重新填写');
        }
    }
    /**
     * 应用状态修改
     * @param array $id
     */
    public function centerStatus(array $id)
    {
        $maps = ApplyCenterModel::find($id);
        if ($maps) {
            tips('未找到应用信息，修改失败');
        }
        $maps->updated_by = UserAuth::instance()->username();
        $maps->status = $maps->status == 1 ? 0 : 1;
        return $maps->save();
    }
    /**
     * 应用中心删除
     * @param array $ids
     */
    public function centerDelete(array $ids)
    {
        return ApplyCenterModel::whereIn('id', $ids)->delete();
    }
}
